import { ref } from "vue";
import { onLoad } from "@dcloudio/uni-app";
import { useSwipe } from "./swipe";
import { useThis } from "./global";

export const useList = <T = any>(
  query: MiniApp.PagingQuery,
  fn: (query: MiniApp.PagingQuery) => Promise<MiniApp.PagingData>
) => {
  const listData = ref<T[]>([]);
  const listStatus = ref<"more" | "loading" | "noMore">("more");
  const listLoadMore = ref(false);
  const reloadList = () => {
    query.page = 1;
    return fn(query)
      .then(resp => renderList(resp.items, Number(resp.pagination.size)))
      .catch(() => (listStatus.value = "more"));
  };
  const renderList = (items: any[], size: number) => {
    query.page === 1 ? (listData.value = items) : listData.value.push(...items);
    listStatus.value = items.length >= size ? "more" : "noMore";
    listLoadMore.value = listData.value.length >= size;
  };
  const loadMoreList = () => {
    if (listStatus.value !== "more") return Promise.resolve();
    listStatus.value = "loading";
    query.page += 1;
    return fn(query)
      .then(resp => renderList(resp.items, Number(resp.pagination.size)))
      .catch(() => (listStatus.value = "more"));
  };
  return { listData, listStatus, listLoadMore, reloadList, loadMoreList };
};

export const useTabs = (callback?: (index: number) => void) => {
  const tabCacheList: number[] = [];

  const { swipeData, swipeChange } = useSwipe(index => {
    if (tabCacheList.includes(index)) return;
    callback?.(index);
    tabCacheList.push(index);
  });

  const tabClick = (index: number) => {
    swipeData.current = index;
  };

  return { tabData: swipeData, tabCacheList, tabClick, tabChange: swipeChange };
};

// export const useTabs = (
//   option: {
//     list: string[];
//     index?: number;
//   },
//   callback?: (index: number) => void
// ) => {
//   const tabIndex = ref(0);
//   const tabList = ref<string[]>(option.list);
//   const tabCacheList: number[] = [];

//   const tabClick = (index: number) => (tabIndex.value = index);
//   const tabChange = (e: MiniApp.Event<{ current: number }>) => {
//     const index = e.detail.current;
//     console.log("tabChange swipe", index);
//     tabIndex.value = index;
//     if (tabCacheList.includes(index)) return;
//     callback?.(index);
//     tabCacheList.push(index);
//   };

//   // 初始化
//   if (option.index !== undefined) {
//     option.index === 0
//       ? tabChange({ detail: { current: 0 } })
//       : tabClick(option.index);
//   }
//   return { tabIndex, tabList, tabClick, tabChange, tabCacheList };
// };

export const useEventChannel = (callback?: () => void) => {
  let eventChannel: UniApp.EventChannel | undefined = undefined;

  onLoad(() => {
    eventChannel = useThis().getOpenerEventChannel() as UniApp.EventChannel;
    callback?.();
  });

  return {
    once: (e: string, callback: () => void) => {
      eventChannel?.once(e, callback);
    },
    emit: (e: string, data: any) => {
      eventChannel?.emit(e, data);
    },
  } as any as UniApp.EventChannel;
};
